package com.fzrskj.cloud.free.user.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.core.utils.DataRangeUtils;
import com.fzrskj.cloud.free.user.bean.UserServiceRecord;
import com.fzrskj.cloud.free.user.bean.UserServiceRecordView;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserServiceRecordDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public UserServiceRecordDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	//to add new record to database by bean
	public int addUserServicerecord(UserServiceRecord obj){
		String sql = "insert into user_service_record(id,userId,userName,instanceId,instanceName,publicIP,operateType,"+
			"operateTime,remark)"+
			"values(?,?,?,?,?,?,?,?,?);";
		Object[] prm={obj.getId(),obj.getUserId(),obj.getUserName(),obj.getInstanceId(),obj.getInstanceName(),obj.getPublicIP(),obj.getOperateType(),
				obj.getOperateTime(),obj.getRemark()};
		int ret = db.executeSql(sql,prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to add new record to database by map
	public int addUserServicerecord(Map<String, Object> values){
		int ret = db.insertRecord("user_service_record",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delUserServicerecord(int id){
		String sql="delete from user_service_record where id=?";
		int ret = db.executeSql(sql,new Object[]{id});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delUserServicerecord(Map<String, Object> cdt){
		int ret = db.deleteRecord("user_service_record",cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modUserServicerecord(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("user_service_record", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query user_service_record item for detail
	public UserServiceRecordView queryUserServicerecord(int id){
		StringBuffer sb = new StringBuffer("select usr.*,");
		sb.append("(select t.description from dic_paramindex n,dic_paramitem t where t.paramId=n.paramEName  and n.paramEName='usOptType' and t.value= usr.operateType) as operateTypeName ");
		sb.append("from user_service_record usr where id=? ");
		UserServiceRecordView ret = db.queryForBean(sb.toString(),UserServiceRecordView.class,new Object[]{id});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	
	//获取符合条件的第一条数据
	public UserServiceRecord queryUsrFirstData(Map<String, Object> cdt){
		StringBuilder sb=new StringBuilder("select * from user_service_record where 1=1 ");

		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}

		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		sb.append(" order by operateTime desc limit 1");

		UserServiceRecord ret = db.queryForBean(sb.toString(),UserServiceRecord.class,params);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query UserServicerecord record count
	public int queryUserServicerecord(Map<String, Object> cdt){
		//TODO please verify count field name
		int ret = db.queryRecordCount("user_service_record", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	/**
	 * 查询符合条件的操作记录
	 * @param cdt
	 * @return
	 */
	public int queryUsRecordCount(Map<String, Object> cdt){
		List<Object> list = new ArrayList<Object>();
		StringBuilder sb=new StringBuilder("select count(id) from user_service_record where 1=1 ");
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			if(c.getKey().equals("start")){
				sb.append(" and operateTime >=?");
				list.add(c.getValue());
			}else if(c.getKey().equals("end")){
				sb.append(" and operateTime <?");
				list.add(c.getValue());
			}else{
				sb.append(" and "+c.getKey()+"=?");
				list.add(c.getValue());
			}
		}

		//数据隔离
		DataRangeUtils.formatRangeSql(sb, " userId ", list);
		
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}

		Long ret = (Long) db.queryFieldValue(sb.toString(), Long.class, params, 1);
		this.setErrCode(db.getErrCode());
		return ret.intValue();
	}

	//to query UserServicerecord for record list with condition,if page or pagesize less than 0, means without page
	public List<UserServiceRecordView> queryUsRecordList(Map<String, Object> cdt, int page, int pagesize){
		StringBuilder sb = new StringBuilder("select usr.*,");
		sb.append("(select t.description from dic_paramindex n,dic_paramitem t where t.paramId=n.paramEName  and n.paramEName='usOptType' and t.value= usr.operateType) as operateTypeName ");
		sb.append("from user_service_record usr where 1=1 ");
		
		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			if(c.getKey().equals("start")){
				sb.append(" and operateTime >=?");
				list.add(c.getValue());
			}else if(c.getKey().equals("end")){
				sb.append(" and operateTime <?");
				list.add(c.getValue());
			}else{
				sb.append(" and "+c.getKey()+"=?");
				list.add(c.getValue());
			}
		}

		//数据隔离
		DataRangeUtils.formatRangeSql(sb, " usr.userId ", list);
		
		sb.append(" order by operateTime desc");
		if ((page>0) && (pagesize>0)) {
			sb.append(String.format(" limit %d,%d",(page-1)*pagesize,pagesize));
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		
		List<UserServiceRecordView> ret = db.queryForBeans(sb.toString(),UserServiceRecordView.class,params);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
}
